package com.securities.stock.service.impl;

import com.github.pagehelper.PageHelper;
import com.securities.stock.dao.StockDao;
import com.securities.stock.entity.pojo.Stock;
import com.securities.stock.enums.StationEnum;
import com.securities.stock.mapper.StockMapper;
import com.securities.stock.service.StockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockServiceImpl implements StockService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StockServiceImpl.class);

    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockMapper stockMapper;


    public List<Stock> selectAll(){
        PageHelper.startPage(1, 10);
        PageHelper.orderBy("created desc ");
        return 	stockDao.selectAll();

    }



    public  void initAllStcok(){
        LOGGER.info("开始执行初始化stcok 代码======= start stcok============");
        String sh = StationEnum.SH.getCode();
        String shCodePrefix600="600";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix600+i);
            if(i < 100){
                stock.setCode(shCodePrefix600+"0"+i);
            }
            stock.setStation(sh);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String shCodePrefix601="601";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix601+i);
            if(i < 100){
                stock.setCode(shCodePrefix601+"0"+i);
            }
            stock.setStation(sh);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String shCodePrefix603="603";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix603+i);
            if(i < 100){
                stock.setCode(shCodePrefix603+"0"+i);
            }
            stock.setStation(sh);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String shCodePrefix605="605";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix605+i);
            if(i < 100){
                stock.setCode(shCodePrefix605+"0"+i);
            }
            stock.setStation(sh);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String sz = StationEnum.SZ.getCode();
        String shCodePrefix000="000";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix000+i);
            if(i < 100){
                stock.setCode(shCodePrefix000+"0"+i);
            }
            stock.setStation(sz);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String shCodePrefix001="001";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix001+i);
            if(i < 100){
                stock.setCode(shCodePrefix001+"0"+i);
            }
            stock.setStation(sz);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }
        String shCodePrefix002="002";
        for(int i=11;i<999;i++){
            Stock stock=new Stock();
            stock.setCode(shCodePrefix002+i);
            if(i < 100){
                stock.setCode(shCodePrefix002+"0"+i);
            }
            stock.setStation(sz);
            int count = stockMapper.selectCount(stock);
            if(count==0){
                stockMapper.insertSelective(stock);
            }
        }

        LOGGER.info("执行完毕初始化stcok 代码======= end stcok============");

    }

}
